package simulation;

import java.util.HashMap;
import java.util.Map;

/**
 * 数青蛙（模拟，不好想）
 */
public class Demo20250606 {
    // 方法一
    public int minNumberOfFrogs(String c) {
        char[] croakOfFrogs = c.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];

        Map<Character, Integer> index = new HashMap<>(); // 存[x, x这个字符对应的下标]
        for (int i = 0; i < n; i++) {
            index.put(t.charAt(i), i);
        }

        for (char ch : croakOfFrogs) {
            if (ch == t.charAt(0)) {
                if (hash[n - 1] != 0) {
                    hash[n - 1]--;
                }
                hash[0]++;
            } else {
                int i = index.get(ch);
                if (hash[i - 1] == 0) {
                    return -1;
                }
                hash[i - 1]--;
                hash[i]++;
            }
        }

        for (int i = 0; i < n - 1; i++) {
            if (hash[i] != 0) {
                return -1;
            }
        }

        return hash[n - 1];
    }

    // 方法二  快
    public int minNumberOfFrogs2(String c) {
        int[] count = new int[5];
        for (char ch : c.toCharArray()) {
            if (ch == 'c') {
                if (count[4] != 0) {
                    count[4]--;
                }
                count[0]++;
            }
            if (ch == 'r') {
                if (count[0] == 0) {
                    return -1;
                }
                count[0]--;
                count[1]++;
            }
            if (ch == 'o') {
                if (count[1] == 0) {
                    return -1;
                }
                count[1]--;
                count[2]++;
            }
            if (ch == 'a') {
                if (count[2] == 0) {
                    return -1;
                }
                count[2]--;
                count[3]++;
            }
            if (ch == 'k') {
                if (count[3] == 0) {
                    return -1;
                }
                count[3]--;
                count[4]++;
            }

        }
        for (int i = 0; i < 4; i++) {
            if (count[i] != 0) {
                return -1;
            }
        }
        return count[4];
    }
}
